package tps.video_games;

import java.awt.Color;
import java.util.List;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.GameComponent;
import com.uqbar.vainilla.GameScene;
import com.uqbar.vainilla.appearances.Circle;
import com.uqbar.vainilla.appearances.Rectangle;
import com.uqbar.vainilla.colissions.CollisionDetector;

public class Bala extends GameComponent<GameScene>{
	
	int width = 4;
	int height = 10;
	Paleta paleta;
	
	public Bala(Paleta p) {
		this.paleta = p;
		this.setX(p.getX()+p.getWidth()/2);
		this.setY(p.getY());
		this.setAppearance(new Rectangle(Color.PINK, this.width, this.height));
	}
	
	
	@Override
	public void update(DeltaState deltaState) {
		this.move(0,-40);
		if(this.colisionaConLadrillo()){
			this.destroy();			
		}else if(this.getY()<0)
			this.destroy();	
		
		super.update(deltaState);
	}
	
	//CHEQUEO DE COLISIONES (PARECIDO A LA BOLITA)
	public boolean colisionaConRectangulo(GameComponent<GameScene> r){
		return CollisionDetector.INSTANCE.collidesRectAgainstRect(this.getX(),this.getY(), this.width, this.height,r.getX(),r.getY(),(int)r.getAppearance().getWidth(),(int)r.getAppearance().getHeight());
	}
	
	public boolean colisionaConLadrillo() {
		boolean acerto = false; 
		Arkanoid arkanoid = (Arkanoid) this.getGame();
		List<Ladrillo> ladrillos = arkanoid.getLadrillos();
		
		Ladrillo ladrilloConElQueChoco = null;
		for(Ladrillo ladrillo : ladrillos){
			if(this.colisionaConRectangulo(ladrillo)){
				ladrillo.recibirGolpe();
				ladrilloConElQueChoco = ladrillo;
				acerto = true;
			}
		}
		if(acerto && ladrilloConElQueChoco.esDestruido())
			ladrillos.remove(ladrilloConElQueChoco);
		return acerto;
	}

}
